package com.kuang.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luomingliang
 * @date 2021/5/25 21:19
 */
public class Demo1 {

    /**
     * 现在有5个用户，进行筛选：
     *      1、ID必须是偶数
     *      2、年龄必须大于23岁
     *      3、用户名转为大写
     *      4、用户名字母倒着排序
     *      5、只输出一个用户
     */
    public static void main(String[] args) {
        User u1 = new User(1, "a", 21);
        User u2 = new User(2, "b", 22);
        User u3 = new User(3, "c", 23);
        User u4 = new User(4, "d", 24);
        User u5 = new User(6, "e", 25);

        //Stream流式编程
        List<User> users = Arrays.asList(u1, u2, u3, u4, u5);
        User user1 = users.stream()
                .filter(u -> u.getId()%2 == 0 && u.getAge() > 23)
                .peek(u -> u.setName(u.getName().toUpperCase()))
                .sorted((x,y) -> y.getName().compareTo(x.getName()))
                .limit(1)
                .collect(Collectors.toList()).get(0);
        System.out.println(user1);
    }
}
